package leetCode.thirdWeek.stack;

import java.util.Stack;

public class Code_71 {
  public String simplifyPath(String path) {
    String[] splitTmp = path.split("/");
    Stack<String> tmp = new Stack<>();
    for (int i = 0; i < splitTmp.length; i++) {
      if (splitTmp[i].equals("")
          | (tmp.empty() && splitTmp[i].equals(".."))
          | splitTmp[i].equals(".")) {
        continue;
      }
      if (splitTmp[i].equals("..")) {
        tmp.pop();
        continue;
      }
      tmp.push(splitTmp[i]);
    }

    String res = "";
    for (int i = 0; i < tmp.size(); i++) {
      res += "/" + tmp.get(i);
    }
    return res == "" ? "/" : res;
  }
}
